<?php

class WebResponse {
	public function __construct() {}
	
	private $state = '200 OK';
	private $headers = array();
	private $body;
	
	public static function makeNew() {
		return new WebResponse();
	}
	
	public function setHeaders(array $arr) {
		$this->headers = array();
		foreach ($arr as $name => $value)
			$this->headers[$name] = $value;
			
		return $this;
	}
	
	public function setHeader($name, $value) {
		$this->headers[$name] = $value;
		return $this;
	}
	
	public function setState($state) { $this->state = $state; return $this; }
	
	public function setBody($body) { $this->body = $body; return $this; }
	
	public function body() { return $this->body; }
	public function state() { return $this->state; }
	public function headers() { return $this->headers; }
	
	public function render() {
		@header('HTTP/1.1 ' . $this->state());
		foreach ($this->cookies() as $name => $value) {
			if (is_object($value))
				$value->renderCookie();
			else
				setcookie($name, $value);
		}
		
		foreach ($this->headers() as $name => $value)
			@header("$name: $value");
		$this->renderBody();
	}
	
	public function renderBody() {
		$body = $this->body();
		if (!is_null($body)) echo $body;
	}
	
	private $Cookies = array();

	public function cookies() {
		return $this->Cookies;
	}

	public function setCookies(array $newValue) {
		$this->Cookies = $newValue;
		return $this;
	}

	public function setCookie($name, $value) {
		$this->Cookies[$name] = $value;
		return $this;
	}
}

?>